package com.treasure.service.loan.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.treasure.service.annotation.LogInfo;
import com.treasure.service.dto.LoanDto;
import com.treasure.service.dto.TLoanOrderDto;
import com.treasure.service.enums.CodeEnum;
import com.treasure.service.loan.service.LoanService;
import com.treasure.service.third.utils.RestClient;
import com.treasure.service.third.utils.TokenConfig;
import com.treasure.service.third.utils.UserToken;
import com.treasure.service.result.PageResult;
import com.treasure.service.result.ResponseResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/** 
* @ClassName    : LoanController 
* @author       : panxingwu 
* @date         : 2018年12月9日 下午1:44:21 
* @Description  : 贷款业务
*/
@Api(value = "贷款业务",description = "贷款业务")
@RestController
@RequestMapping("/loan/v1")
@Slf4j
public class LoanController {

    @Autowired
    private RestClient restClient;
    @Autowired
    private LoanService loanService;

    @ApiOperation(value = "获取贷款列表", notes = "获取贷款列表")
    @PostMapping("/getLoanList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "頁碼", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每頁顯示數量", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "当前状态(0-禁用 1-启用)", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "recommend", value = "是否首页展示(0-否 1-是)", required = true, dataType = "int", paramType = "query"),
    })
    @LogInfo(log = "获取贷款列表",isPrint = true)
    public ResponseEntity<ResponseResult> getLoanList(@RequestParam(value = "pageSize") Integer pageSize,
                                         @RequestParam(value = "pageNum") Integer pageNum,
                                         @RequestParam(value = "status",required = false) Integer status,
                                         @RequestParam(value = "recommend",required = false) Integer recommend){
    	List<LoanDto> hotList = new ArrayList<LoanDto>();
    	List<LoanDto> list = new ArrayList<LoanDto>();
    	PageResult<List<LoanDto>> loanList = loanService.getLoans();
    	List<LoanDto> loans = loanList.getRows();
    	for (Iterator iterator = loans.iterator(); iterator.hasNext();) {
    		LoanDto tLoan = (LoanDto) iterator.next();
			if("Y".equals(tLoan.getIsHot())) {
				hotList.add(tLoan);
			}else {
				list.add(tLoan);
			}
		}
        Map<String,List<LoanDto>> pageResult = new HashMap<String,List<LoanDto>>();
        pageResult.put("hot", hotList);
        pageResult.put("list", list);
    	return ResponseEntity.ok(new ResponseResult(CodeEnum.SUCCESS,pageResult));
    }
    
    @ApiOperation(value = "获取贷款申请地址", notes = "获取贷款申请地址")
    @PostMapping("/getApplyUrl")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "realName", value = "姓名", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "certNo", value = "身份证", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "mobile", value = "手机号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "loanid", value = "贷款ID", required = true, dataType = "String", paramType = "query"),
    })
    @LogInfo(log = "获取贷款申请地址",isPrint = true)
    public ResponseEntity<ResponseResult> getApplyUrl(@RequestParam(value = "realName",required = true) String realName,
			                                          @RequestParam(value = "certNo",required = true) String certNo,
			                                          @RequestParam(value = "mobile",required = true) String mobile,
			                                          @RequestParam(value = "loanid",required = true) String loanid){
    	UserToken user = TokenConfig.getTokenByHeader();
    	log.info("token"+JSON.toJSONString(user));
    	TLoanOrderDto tLoanOrderDto = new TLoanOrderDto();
    	tLoanOrderDto.setCustomerId(user.getUserId());
    	tLoanOrderDto.setRealName(realName);
    	tLoanOrderDto.setCertNo(certNo);
    	tLoanOrderDto.setMobile(mobile);
    	tLoanOrderDto.setLoanId(loanid);
    	return ResponseEntity.ok(new ResponseResult(CodeEnum.SUCCESS,loanService.createOrder(tLoanOrderDto)));
    }
}
